<?php
error_reporting(0);
define('IN_PISCES',true);
define('PISCES_BASE_URL','http://pisces/');
date_default_timezone_set('Etc/GMT-8');

$piscesHome 	= 'D:/www/pisces/web/';

$projHome 		= $piscesHome.'pisces/';
include $projHome . 'framework/runtime/Lotus.php';

$lotus = new Lotus();

$lotus->option['proj_dir'] = $projHome.'code/';

$lotus->option['app_dir'] = $projHome.'code/app/';

$lotus->option['app_name'] = 'frontend';

$lotus->option['load_function'] = true;
$lotus->option['runtime_filemap'] = true;

$lotus->mvcMode = false;
$lotus->devMode = true;
$lotus->init();

function taozhangmen_product_list($condition,$template){
	$tmp	= taozhangmen_parse_condition($condition);
	if(empty($tmp))
	{
		return false;
	}
	extract($tmp);
	$con	= array();
	if(is_numeric($cat) && $cat>0)
	{
		$con['cid']	= $cat;
	}
	if(!empty($keyword))
	{
		$con['keyword']	= $keyword;
	}
	if(!empty($ids))
	{
		$con['ids']	= $ids;
	}
	if(!is_numeric($num) || $num<=0 || $num>20)
	{
		$num = 10;
	}
	$con['page_size']	= $num;
	if(empty($fields))
	{
		$fields	= array();
	}
	else
	{
		$fields	= explode(',',$fields);
	}
	$ret = C('ProductService')->getList($con,$fields,$cache,$position);
	if(!empty($ret))
	{
		$returnSetting = C('SettingService')->get('return_setting',true);
		$url	= C('LtUrl');
		$cf		= C('CommonFunction');
		foreach($ret as &$rv)
		{
			if($returnSetting['rate']>0 && array_key_exists('commission',$rv))
			{
				$rv['commission']	= round($rv['commission']*$returnSetting['rate']/100,2);
			}
			$rv['detail_link']	= $url->generateOutsiteUrl(PISCES_BASE_URL,'Default','View',array('id'=>$cf->getId($rv['num_iid'])));
			$rv['click_link']	= $url->generateOutsiteUrl(PISCES_BASE_URL,'Default','Click',array('id'=>$rv['num_iid']));
			if(array_key_exists('pic_url',$rv))
			{
				$rv['image']	= base64_encode($rv['pic_url']);
				$rv['image_b']	= base64_encode($rv['pic_url'].'_b.'.$cf->getFileExt($rv['pic_url']));
			}
		}
	}
	$result = '';
	if(!empty($ret))
	{
		foreach($ret as $v)
		{
			$tmp	= $template;
			$tmp	= str_replace('[taozhangmen.detail_link]',$v['detail_link'],$tmp);
			$tmp	= str_replace('[taozhangmen.click_link]',$v['click_link'],$tmp);
			$tmp	= str_replace('[taozhangmen.image]',$v['image'],$tmp);
			$tmp	= str_replace('[taozhangmen.image_b]',$v['image_b'],$tmp);
			$tmp	= str_replace('[taozhangmen.commission]',$v['commission'],$tmp);
			$tmp	= str_replace('[taozhangmen.title]',$v['title'],$tmp);
			$tmp	= str_replace('[taozhangmen.item_location]',$v['item_location'],$tmp);
			$tmp	= str_replace('[taozhangmen.price]',$v['price'],$tmp);
			$result	.= $tmp;
		}
	}
	
	echo $result;
}

function taozhangmen_parse_condition($str){
	$tmp = preg_split('/\s+/',$str);
	if(empty($tmp))
	{
		return false;
	}
	$con	= array();
	foreach($tmp as $v)
	{
		$tt = explode('=',$v);
		if(count($tt)==2 && in_array($tt[0],array('cat','keyowrd','ids','num','cache','position','fields')))
		{
			$con[$tt[0]]	= str_replace('"','',str_replace("'",'',$tt[1]));
		}
	}
	return $con;
}